package panel.newsDetail
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.TimeToStr;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import inerface.INewsDetailPanel;
	
	import main.ClientConstants;
	
	import panel.viewSpots.navigation.ScrollTxtHor;
	import panel.viewSpots.navigationNew.PicSlideGroup;
	
	public class NewsDetailPanel extends Sprite implements INewsDetailPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var _picGroup:PicSlideGroup;
		
		private var titleText:ScrollTxtHor;
		private var contentText:TextField;
		
		private var thisObj:Object;
		
		private var imgList:Array;
		
		private var sourceList:Array;
		
		public function NewsDetailPanel()
		{
			sourceList = [];
			imgList = [];
		}
		
		public function changeData(obj:Object):void
		{
			thisObj = obj;
			
			addPic();
			addText();
		}
		
		private function addText():void
		{
//			titleText.y = _picGroup.y + _picGroup.height + 5;
			
//			TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor(thisObj.newsTitle,0xFFFF00,18),true,true);
			
			titleText.label(HtmlText.formatSizeColor(thisObj.newsTitle,0xFFFF00,20));
			
//			titleText.width = titleText.textWidth + 5;
//			titleText.height = titleText.textHeight + 5;
			
			titleText.x = (_stage.stageWidth - titleText.width) / 2;
			
			TextFieldUtils.setHtmlText(contentText,HtmlText.formatSizeColor(thisObj.newsContent + "     \n" + TimeToStr.seperateDate(thisObj.newsDate),0xFFFFFF,16),true,false);
			contentText.height = _stage.stageHeight * .52 - titleText. height - 10;
			contentText.y = titleText.y + titleText.height + 5;
		}
		
		private function addPic():void
		{
			sourceList = thisObj.newsUrl.split("^");
			imgList = [];
			
//			var bmp:ImageResource;
//			for(var i:int = 0;i < sourceList.length;i++){
//				if(i != sourceList.length - 1){
//					bmp = new ImageResource(_loader,_uri.getNewsImg(sourceList[i]));
//				}else{
//					bmp = new ImageResource(_loader,_uri.getNewsImg(sourceList[i]),reposition);
//				}
//				
//				imgList.push(bmp);
//			}
			
			startLoadImg(0);
		}
		
		private function startLoadImg(i:int):void
		{
			var img:ImageResource = new ImageResource(_loader,_uri.getNewsImg("other/",sourceList[i]),endAndStart);
			
			function endAndStart():void
			{
				var bitmap:Bitmap = new Bitmap(img.imageData);
				
				bitmap.width = _stage.stageWidth;
				bitmap.height = _stage.stageHeight * 0.3;
				
				imgList.push(bitmap);
				
				if(i < sourceList.length - 1){
					startLoadImg(i + 1);
				}else{
					reposition();
				}
			}
		}
		
		private function reposition():void
		{
			_picGroup.show(imgList);
			this.addChild(_picGroup);
//			repoText();
		}
		
		private function repoText():void
		{
			titleText.y = _picGroup.y + _picGroup.height + 5;
			contentText.y = titleText.y + titleText.height + 5;
			
			contentText.height = _stage.stageHeight * .9 - contentText.y - 20;
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			addContent();
		}
		
		private function addContent():void
		{
			_picGroup = new PicSlideGroup();
			_picGroup.init(_stage,_loader);
			
			_picGroup.x = 0;
			_picGroup.y = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			
			titleText = new ScrollTxtHor(TextFieldAutoSize.CENTER);
			titleText.showWid = _stage.stageWidth * .8;
			titleText.x = _stage.stageWidth * .1;
			titleText.y = _stage.stageHeight * .08 + _stage.stageHeight * .3 + 5;
			this.addChild(titleText);
			
			contentText = new TextField;
			contentText.multiline = true;
			contentText.wordWrap = true;
			contentText.selectable = false;
			contentText.width = _stage.stageWidth * .9;
			
			this.addChild(contentText);
			contentText.x = _stage.stageWidth * .05;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
			}
		}
	}
}